#pragma once

#include "gdiplus.h"
//using namespace Gdiplus;  
#pragma comment(lib, "gdiplus.lib")

enum DRAW_STYLE
{
	DS_NORMAL = 0,
	DS_KEYCOLOR = 1, // Only RGB(255, 0, 255) will be treated as key color!
	DS_ALPHA = 2,
	DS_INVERT = 4,
};

/*
	Description: ImageList class that support both bitmap and PNG.
	Usage: Similar with WTL CImageList.

	Note: Make sure you have initialized GDI+ before use this class!
*/
class XTREMEFRAMEPRODLLEXPORT CImageListEx
{
public:
	CImageListEx(void);
	virtual ~CImageListEx(void);

public:
	int 
		Attach(Gdiplus::Bitmap* pBitmap, int nIconCount, bool bManaged = TRUE);
	void
		Detach();

	int 
		Load(HINSTANCE hInst, int resID, int nImageCount
			, LPCWSTR pszResType = RT_BITMAP);
	int
		Load(LPCWSTR pszFileName, int nImageCount);
	int 
		Load(HBITMAP hBitmap, int nImageCount);

	int 
		Destroy();

	/*
	Parameters,
		dc			Dest device context
		nImageIndex Zero based image index
		destx		Dest x position
		desty		Dest y position
		drawStyle	can be any combination of DRAW_STYLE
		alpha		If drawStyle contains DS_ALPHA, will use this value as alpha. 
					Range 0 ~ 1
	*/
	int 
		Draw(HDC dc, int nImageIndex, int destx, int desty
				, DWORD drawStyle = DS_NORMAL, float alpha = 1);

	/* 
	Note: 
		As GDI+ will dithering the bitmap for display, the bitmap may include 
		unexpected color near the boundary. You can use nAdjustWidth(-1) to adjust 
		source width slightly to obtain a better display.
	*/
	int StrechDraw(HDC dc, int nImage, int destx, int desty
					, int destWidth, int destHeight
					, DWORD drawStyle = DS_NORMAL
					, float alpha = 1, int nAdjustWidth = 0);

	Gdiplus::Bitmap*
		GetBitmap();
	bool
		GetImageSize(int& width, int& height);
	int 
		GetImageCount();

	// Utility for load PNG
	static bool 
		CreateBitmapFromPNG(HINSTANCE hInst, UINT nID
						, LPCTSTR pszResType, Gdiplus::Bitmap*& pBitmap);

	//------
	operator bool () const { return _pBitmap!= NULL; }

protected:
	Gdiplus::Bitmap* 
		_pBitmap;

	int
		_imageWidth;
	int
		_imageHeight;
	int 
		_nImageCount;

	int 
		_bManaged;

protected:
	void 
		_disposeBitmap(bool bManaged);

	int _drawImageWithStyle(Gdiplus::Graphics& graphics
							, int x, int y, Gdiplus::Rect& destRect
							, int srcx , DWORD drawStyle
							, float alpha);
};


